﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.QuanLyKho;

namespace QuanLyKho.QuanLyKho
{
    public partial class CapNhatXuatKho : UserControl
    {
        public CapNhatXuatKho()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvXuatkho.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvXuatkho.OptionsView.ShowFooter = true;
            gvXuatkho.OptionsFind.AlwaysVisible = true;
            gcID.Visible = false;

            gvDuyet.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvDuyet.OptionsView.ShowFooter = true;
            gvDuyet.OptionsFind.AlwaysVisible = true;
            gcID1.Visible = false;
        }

        private void LoadData()
        {
            gridXuatkho.DataSource = Data.DataAll.SelectAll("XuatKho_VatTu inner join Kho_KhoHang on XuatKho_VatTu.[MaPhong] = Kho_KhoHang.[IDKhoHang]",
                "Kho_KhoHang.[IDKhoHang] as 'KhoNhan', Kho_KhoHang.[TenKho] as MaPhong,XuatKho_VatTu.[ID],XuatKho_VatTu.[NgayXuat],XuatKho_VatTu.[NguoiLapPhieu],XuatKho_VatTu.[NgayChungTu],XuatKho_VatTu.[NgayHoachToan],XuatKho_VatTu.[SoChungTu],XuatKho_VatTu.[GhiChu],XuatKho_VatTu.[NguoiNhan]",
                "WHERE TrangThai= " + Convert.ToInt32(TrangThaiKho.XuatKho_ChoDuyet));
            gridDuyet.DataSource = Data.DataAll.SelectAll("XuatKho_VatTu inner join Kho_KhoHang on XuatKho_VatTu.[MaPhong] = Kho_KhoHang.[IDKhoHang]",
                "XuatKho_VatTu.NguoiCapNhat,Kho_KhoHang.[TenKho] as MaPhong,XuatKho_VatTu.[ID],XuatKho_VatTu.[NgayXuat],XuatKho_VatTu.[NguoiLapPhieu],XuatKho_VatTu.[NgayChungTu],XuatKho_VatTu.[NgayHoachToan],XuatKho_VatTu.[SoChungTu],XuatKho_VatTu.[GhiChu],XuatKho_VatTu.[NguoiNhan]",
                "WHERE TrangThai= " + Convert.ToInt32(TrangThaiKho.XuatKho_DaDuyet));

        }

        private void gvXuatkho_DoubleClick(object sender, EventArgs e)
        {
            Kho_Themxuatkho.ID_XuatKho = gvXuatkho.Columns["ID"].View.GetRowCellDisplayText(gvXuatkho.FocusedRowHandle, "ID");
            Kho_Themxuatkho xuatkho = new Kho_Themxuatkho();
            xuatkho.ShowDialog();
            LoadData();
            Kho_Themxuatkho.ID_XuatKho = "0";
        }

        private void gvDuyet_DoubleClick(object sender, EventArgs e)
        {
            Kho_Themxuatkho.ID_XuatKho = gvDuyet.Columns["ID"].View.GetRowCellDisplayText(gvDuyet.FocusedRowHandle, "ID");
            Kho_Themxuatkho xuatkho = new Kho_Themxuatkho();
            xuatkho.ShowDialog();
            LoadData();
            Kho_Themxuatkho.ID_XuatKho = "0";
        }

        private void btnGuiPhieu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvXuatkho.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn cập nhật lệnh xuất kho này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                DataTable dtXuatKhoChiTiet = DataAll.SelectAll("XuatKho_VatTu_ChiTiet", "*", "WHERE ID_XuatKho= " + dataview[gvXuatkho.FocusedRowHandle]["ID"]);
                for (int i = 0; i < dtXuatKhoChiTiet.Rows.Count; i++)
                {
                    string id_vattu = dtXuatKhoChiTiet.Rows[i]["IDVatTu"].ToString();
                    string soluongXuat = dtXuatKhoChiTiet.Rows[i]["SoLuong"].ToString();

                    var dtVatTu = DataAll.SelectAll("Kho_VatTu", "*", " WHERE IDVatTu=" + id_vattu);
                    var dtKhoAo = DataAll.SelectAll("Kho_VatTuDuKien", "*",
                                                    " WHERE MaVatTu=N'" + dtVatTu.Rows[0]["MaVatTu"] + "'");
                    double soluong = Convert.ToDouble(dtVatTu.Rows[0]["SoLuong"]) - Convert.ToDouble(soluongXuat);
                    double soluongKhoAo = Convert.ToDouble(dtKhoAo.Rows[0]["SoLuong"]) - Convert.ToDouble(soluongXuat);
                    DataAll.Update("Kho_VatTu", "SoLuong = " + soluong + " WHERE IDVatTu=" + id_vattu);
                    DataAll.Update("Kho_VatTuDuKien",
                                   "SoLuong = " + soluongKhoAo + " WHERE MaVatTu=N'" + dtVatTu.Rows[0]["MaVatTu"] + "'");

                    // cập nhật kho nhận
                    double soLuongKhoNhan = Convert.ToDouble(DataAll.SelectAll("Kho_KhoHang_VatTu", "*",
                        "WHERE IDVatTu=" + id_vattu + "AND IDKhoHang = " + dataview[gvXuatkho.FocusedRowHandle]["KhoNhan"]).Rows[0]["SoLuong"].ToString()) + Convert.ToDouble(soluongXuat);

                    DataAll.Update("Kho_KhoHang_VatTu", "SoLuong = " + soLuongKhoNhan + " WHERE IDVatTu=" + id_vattu + "AND IDKhoHang = " + dataview[gvXuatkho.FocusedRowHandle]["KhoNhan"]);
                }
                var a = dataview[gvXuatkho.FocusedRowHandle]["ID"];
                DataAll.Update("XuatKho_VatTu",
                                   "TrangThai = " + Convert.ToInt32(TrangThaiKho.XuatKho_DaDuyet) + ",NguoiCapNhat = '" + Main.UserID + "' WHERE ID=" + a);
            }
            MessageBox.Show("Cập nhật lệnh xuất kho thành công", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            LoadData();
        }

        private void btnKhongCapNhat_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvXuatkho.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn từ chối cập nhật lệnh xuất kho này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                DataAll.Update("XuatKho_VatTu",
                                 "TrangThai = " + Convert.ToInt32(TrangThaiKho.XuatKho_KhongDuyet) + " WHERE ID=" + dataview[gvXuatkho.FocusedRowHandle]["ID"]);
            }
            LoadData();
            MessageBox.Show("Từ chối cập nhật lệnh xuất kho thành công", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
